$(function() {
    $('form').each(function() {
        var $form = $(this);
        var $action = $form.attr('action');
        if ($action.indexOf('edit') !== -1 || $action.indexOf('add') !== -1) {
            
            $form.attr({
                'name': 'adminForm',
                'id': 'adminForm',
                'onsubmit': 'return false'
            });

            var msg = $action.indexOf('edit') !== -1 ? _lang.confirm_edit : _lang.confirm_add;
            $('#save').click(function() {
                if (confirm(msg)) {
                    document.adminForm.submit();
                }
            });
        }
    });
});

function saveorder() {
    task = $("#action").val();
    if (task == 0) {
        alert("Please Select Action");
        return false;
    }
    $("form").submit()
}

function scrollTop() {
    $("html, body").animate({
        scrollTop: 0
    }, "slow")
}

function change_status(a, b, c, d) {
    $.ajax({
        url: b + "/changestatus",
        data: "id=" + c + "&type=" + d,
        type: "POST",
        success: function(b) {
            if (b >= 0) {
                if (b == 1) {
                    image = "ico_active.png"
                } else {
                    image = "ico_inactive.png"
                }
                a.src = skins_admin + "/images/" + image
            } else {
                InitNotifications("error", "Change status fail")
            }
        }
    })
}

function do_action(a) {
    task = $("#action").val();
    if (task == 0) {
        alert("Please Select Action");
        return false;
    }
    var b = [];
    $(":checkbox:checked").each(function(a) {
        b[a] = $(this).val()
    });
    $("form").submit()
    return true
}

function remove(item, a, b) {
    if (confirm_del()) {
        $.ajax({
            type: "POST",
            url: a,
            data: "id=" + b,
            success: function(result) {
                if (result > 0) {
                    $('#' + item).addClass("row_selected");
                    var anSelected = fnGetSelected(oTable);
                    oTable.fnDeleteRow(anSelected[0]);
                } else {
                    alert('Delete Error!!');
                    //                    InitNotifications("error","Delete Error");
                }
            }
        })
    }

}

function removeChapter(item, a, b, c) {
    if (confirm_del()) {
        $.ajax({
            type: "POST",
            url: a,
            data: "id=" + b + "&parent_id=" + c,
            success: function(result) {
                if (result > 0) {
                    $('#' + item).addClass("row_selected");
                    $('tr[rel=' + item + ']').addClass("row_selected");
                    var anSelected = fnGetSelected(oTable);
                    for (i = 0; i < anSelected.length; i++)
                        oTable.fnDeleteRow(anSelected[i]);
                } else {
                    alert('Delete Error!!');
                    //                    InitNotifications("error","Delete Error");
                }
            }
        })
    }

}

function backup_tplaylist(item, a, b) {
    if (confirm('Are you sure you want to backup this playlist?')) {
        $.ajax({
            type: 'post',
            url: a,
            data: 'id=' + b,
            success: function(result) {
                if (result > 0) {
                    alert('Backup successful!');
                } else {
                    alert('Backup failed!');
                }
            },
            error: function(err) {
                alert('System error!');
            }
        })
    }
}